package com.example.nyf.web.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.nyf.common.AjaxResult;
import com.example.nyf.dto.SysMenuDTO;
import com.example.nyf.entity.SysMenu;
import com.example.nyf.service.SysMenuService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

@Controller
public class MenuController extends BaseController {

    @Autowired
    SysMenuService sysMenuService;

    /**
     * 菜单页面
     * @return
     */
    @RequestMapping("/system/menu_list")
    @RequiresPermissions("system:menu:list")
    public ModelAndView menuList() {

        ModelAndView modelAndView = new ModelAndView("system/menu_list");

        return modelAndView;
    }

    /**
     * 返回 菜单层级列表
     * @return
     */
    @RequestMapping("/system/get_menu_list_all")
    @ResponseBody
    public String listSysMenuAll() {
        List<SysMenuDTO> list = sysMenuService.listSysMenuAll();
        return JSON.toJSONString(list);
    }

    /**
     * 菜单详情页面
     * @return
     */
    @RequestMapping("system/add_menu")
    @RequiresPermissions("system:menu:cud")
    public ModelAndView menuInfo(@RequestParam(value = "id", required = false, defaultValue = "0") Integer id) {
        String data = "{}";
        String top_menu_list = "{}";

        SysMenu sysMenu = sysMenuService.getSysMenu(id);
        if(sysMenu != null) {
            data = JSON.toJSONString(sysMenu);
        }
        List<SysMenu> list = sysMenuService.listSysMenuTop();
        if(list != null) {
            top_menu_list = JSON.toJSONString(list);
        }

        ModelAndView modelAndView = new ModelAndView("system/add_menu");
        modelAndView.addObject("data", data);
        modelAndView.addObject("top_menu_list", top_menu_list);
        return modelAndView;
    }


    @RequestMapping("/system/save_sys_menu")
    @RequiresPermissions("system:menu:cud")
    @ResponseBody
    public AjaxResult insertSysMenu(@RequestBody @Valid SysMenu sysMenu, BindingResult result) {
        if (result.hasErrors()) {
            List<ObjectError> list = result.getAllErrors();
            for (ObjectError error : list) {
                throw new RuntimeException(error.getDefaultMessage());
            }
        }

        int row = 0;
        //JSON字符串转换成Java对象
        //SysMenu sysMenu = JSONObject.parseObject(post, SysMenu.class);
        sysMenu.setAdd_user_id(getSysUser().getId());
        sysMenu.setAdd_user_name(getSysUser().getUser_name());
        sysMenu.setAdd_time((int)(System.currentTimeMillis()/1000));
        if(sysMenu.getId() == null || "".equals(sysMenu.getId())) {
            row = sysMenuService.insertSysMenu(sysMenu);
        } else {
            row = sysMenuService.updateSysMenu(sysMenu);
        }

        if(row > 0) {
            return AjaxResult.success("保存成功");
        } else {
            return AjaxResult.error("保存失败");
        }
    }

    @RequestMapping("/system/delete_menu")
    @RequiresPermissions("system:menu:cud")
    @ResponseBody
    public AjaxResult deleteSysMenu(@RequestParam("id") Integer id) {
        int row = sysMenuService.deleteSysMenu(id);
        if(row > 0) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败");
        }
    }
}
